﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using UnityEngine;

[Serializable]
public struct ObjetoStruct
{
    public string Nome;
    public int AtributoNecessario;
    public int QuantidadeNecessaria;
    public Vector2 Posicao;
    public string Textura;
}

public struct ObjetoCenStruct
{
    public string Nome;
    public int AtributoNecessario;
    public int QuantidadeNecessaria;
    public Vector2 Posicao;
    public string Textura;
}

public class SaveGame
{
    public static IAsyncResult resultado;
    public static ObjetoCenStruct obj;
    public static List<ObjetoStruct> listaobj;
    static bool CarregandoJogo = false;

    static public void Save(ObjetoCenStruct ob, List<ObjetoStruct> listaob)
    {
        obj = ob;
        listaobj = listaob;
        GravaJogo();
    }

    static public void GravaJogo()
    {
        string filename = "" + obj.Nome + ".xml";

        // Abre o arquivo, criando-o se necessário
        FileStream stream = File.Open(Directory.GetCurrentDirectory() + "\\" + filename, FileMode.Create);

        // Serializa o objeto (transforma em XML) e grava o arquivo
        XmlSerializer XMLWrite = new XmlSerializer(typeof(ObjetoCenStruct));
        XMLWrite.Serialize(stream, obj);

        Debug.Log("passou " + listaobj.Count);
        XmlSerializer XML = new XmlSerializer(typeof(ObjetoStruct));
        foreach (ObjetoStruct o in listaobj)
        {
            Debug.Log("passou " + o.Posicao.x);
            XML.Serialize(stream, o);
        }

        //  Fecha tudo que usamos
        stream.Close();
    }


    /*static void LerDispositivo(IAsyncResult resultado)
    {
        storageDevice = Guide.EndShowStorageDeviceSelector(resultado);
        if (storageDevice.IsConnected)
        {
            GravaJogo(storageDevice);
        }
    }*/


    /*static public stSaveGame Load()
    {
        stSaveGame ret = new stSaveGame();
        //  In XBox, presents a screen and lets the user show the device to read from
        //  In Windows, refers to a subfolder in MyDocuments
        //    PlayerIndex parameter is optional
        // Set the request flag
        if ((!Guide.IsVisible) && (CarregandoJogo == false))
        {
            CarregandoJogo = true;
            resultado = Guide.BeginShowStorageDeviceSelector(PlayerIndex.One, null, null);
        }


        while (!resultado.IsCompleted)
        {
            //  Espera o device selector ser mostrado
        }

        if ((CarregandoJogo) && (resultado.IsCompleted))
        {
            StorageDevice storageDevice = Guide.EndShowStorageDeviceSelector(resultado);
            if (storageDevice.IsConnected)
            {
                try
                {
                    // Open a storage container
                    StorageContainer dir = storageDevice.OpenContainer("MeuJogo");

                    // Get the path of the save game
                    string filename = Path.Combine(dir.Path, "MeuSaveGame.xml");

                    // Check to see if the save exists
                    if (!File.Exists(filename))
                    {
                        //  No savegame file - set default values
                        ret.Lifes = 3;
                        ret.PlayerPosition = new Vector2(200f, 200f);
                        ret.EnemyPosition = Vector2.Zero;
                        return ret;
                    }

                    // Open and read the data from the file
                    FileStream stream = File.Open(filename, FileMode.OpenOrCreate,
                        FileAccess.Read);

                    XmlSerializer XMLRead = new XmlSerializer(typeof(stSaveGame));
                    ret = (stSaveGame)XMLRead.Deserialize(stream);

                    // Close the file and the container
                    stream.Close();
                    dir.Dispose();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    //  Se deu erro na carga, carrega os valores default
                    ret.Lifes = 3;
                    ret.PlayerPosition = new Vector2(200f, 200f);
                    ret.EnemyPosition = Vector2.Zero;
                    return ret;
                }
            }
            // Reset the request flag
            CarregandoJogo = false;
        }
        return ret;
    }*/


}

